package lt.marsrutai.mobile.android.content;

import lt.marsrutai.mobile.android.R;
import android.app.SearchManager;
import android.content.SearchRecentSuggestionsProvider;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;
import android.provider.BaseColumns;

/**
 * <p>Provides recent stop search queries with additional static suggestion(s).</p>
 * 
 * @author Justas
 *
 */
public class StopSearchRecentSuggestionsProvider extends SearchRecentSuggestionsProvider {
    
    public final static String AUTHORITY = "lt.marsrutai.mobile.android.content.StopSearchRecentSuggestionsProvider";
    public final static int MODE = DATABASE_MODE_QUERIES | DATABASE_MODE_2LINES;
    
    public static final String QUERY_CLOSEST_STOPS = null;
    
    public StopSearchRecentSuggestionsProvider() {
        setupSuggestions(AUTHORITY, MODE);
    }
    
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        Cursor result = super.query(uri, projection, selection, selectionArgs, sortOrder);

        MatrixCursor suggestions = createStaticSuggestions();
        
        // Append query results to the static suggestions
        result.moveToFirst();
        while (!result.isAfterLast()) {
            suggestions.addRow(new Object[] { 
                    result.getLong(result.getColumnIndex(BaseColumns._ID)),
                    android.R.drawable.ic_menu_recent_history,
                    result.getString(result.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_1)),
                    result.getString(result.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_2)),
                    result.getString(result.getColumnIndex(SearchManager.SUGGEST_COLUMN_QUERY)) });
            result.moveToNext();
        }
        
        return suggestions;
    }
    
    /**
     * Create static suggestions
     * 
     * @return
     */
    protected MatrixCursor createStaticSuggestions() {
        MatrixCursor cursor = new MatrixCursor(new String[] { 
                BaseColumns._ID,
                SearchManager.SUGGEST_COLUMN_ICON_1, 
                SearchManager.SUGGEST_COLUMN_TEXT_1, 
                SearchManager.SUGGEST_COLUMN_TEXT_2, 
                SearchManager.SUGGEST_COLUMN_QUERY});

        cursor.addRow(new Object[] { 
                0,
                android.R.drawable.ic_menu_mylocation,
                getContext().getString(R.string.stop_search_suggestion_closest_stops),
                null,
                QUERY_CLOSEST_STOPS, 
                });
        return cursor;
    }
}
